﻿using HRIS.Browser.UI.ConfigReader;
using Iit.Automation.Browsers.Shared;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRIS.Browser.UI.Framework.DesktopWebControls
{
    public class TopMenu : Menu, IConfigurableComponent
    {
        private IBrowser _browser;
        private ConfigProvider _config;

        public void ConfigureMe(IBrowser browser, IitEnums.Environment env)
        {
            _config = ConfigProvider.GetInstance();
            _browser = browser;

            MenuItem = _config.TopMenu.MenuItem;
            Element = (RemoteWebElement)_browser.FindElement(_config.TopMenu.Element);
            SubMenus = ConfigureSubMenus(_config.TopMenu.SubMenus);
        }

        private List<MenuBase<RemoteWebElement>> ConfigureSubMenus(List<MenuBase<By>> configSubMenuItems)
        {
            var menus = new List<MenuBase<RemoteWebElement>>();
            foreach (var menu in configSubMenuItems)
            {
                var subMenu = new Menu(menu.MenuItem, _browser, menu.Element);
                if (menu.SubMenus != null && menu.SubMenus.Count > 0)
                {
                    subMenu.SubMenus = ConfigureSubMenus(menu.SubMenus);
                }

                menus.Add(subMenu);
            }

            return menus;
        }
    }
}
